home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / tools / czesc_3 / pfortune / fortune.c < prev    next >
C/C++ Source or Header  |  1993-09-18  |  6KB  |  175 lines

  1. echo;                           /* Execute to compile with SAS/C v6.x * /
  2.  
  3. SC Fortune.c LINK STARTUP=cres NOSTDIO PARAMETERS=REGISTERS NOSTACKCHECK STRINGMERGE OPTIMIZE OPTIMIZERSIZE
  4. delete Fortune.lnk Fortune.o
  5. quit
  6. */
  7.  
  8. /**
  9.  **     $Filename: Fortune.c $
  10.  **     $Revision: 2.0 $
  11.  **     $Date: 1993/09/18 $
  12.  **
  13.  **     Prints a random fortune out of a database.
  14.  **
  15.  **     © Copyright 1993 Peter Simons, Germany
  16.  **                      All Rights Reserved.
  17.  **/
  18.  
  19. /**************************************************************************
  20.  *                                                                        *
  21.  * Sektion: Macros, Definitions, Includes, Structures                     *
  22.  *                                                                        *
  23.  **************************************************************************/
  24.  
  25. /************************************* Includes ***********/
  26. #include <stdlib.h>
  27. #include <string.h>
  28.  
  29. #include <proto/exec.h>
  30. #include <proto/dos.h>
  31. #include <proto/ptool.h>
  32. #include <dos/dos.h>
  33. #include <libraries/ptoollibrary.h>
  34.  
  35. /************************************* Defines ************/
  36. #define TRUE 1
  37. #define EOF -1
  38.  
  39. #define PRGNAME "Fortune"
  40.  
  41. #define USAGE "%s v2.0 -- written by Peter Simons <simons@peti.GUN.de>\n" \
  42.               "Randomly display a random fortune from a chosen database.\n\n" \
  43.               "USAGE: %s <database>\n"
  44. #define BUFFERSIZE 256
  45. #define MARGIN 80               /* Customize this defines to fulfill
  46.                                  * your needs. */
  47.  
  48. /************************************* Prototypes *********/
  49. void print_rule(int, char *);
  50. void my_exit(int);
  51. int FGetS(char *, BPTR);
  52.  
  53. /************************************* Global Variables ***/
  54. struct PToolLibrary *PToolBase;
  55.  
  56. /**************************************************************************
  57.  *                                                                        *
  58.  * Sektion: Hauptprogramm                                                 *
  59.  *                                                                        *
  60.  **************************************************************************/
  61.  
  62. #ifdef __SASC                   /* Control-C Handling abschalten */
  63. void _CXBRK(void) { }
  64. void chkabort(void) { }
  65. #endif
  66.  
  67. int main(int argc, char *argv[])
  68. {
  69.         char *path = "S:Fortune.data", officialrule[BUFFERSIZE];
  70.         int next, character;
  71.         unsigned long linecount;
  72.         BPTR fp;
  73.  
  74.         if (!(PToolBase = (struct PToolLibrary *) OpenLibrary(PTOOLNAME, 2L)))
  75.                 return 10;
  76.  
  77.         if (!strcmp("?", argv[1]) || argc > 2) {
  78.                 PrintFSimple(USAGE, PRGNAME, PRGNAME);
  79.                 exit(0);
  80.         }
  81.  
  82. /*---- Open "fortunes" file -------------------------------------------*/
  83.         if (argc == 2)
  84.                 path = argv[1]; /* commandline has higher priority */
  85.         if (!((fp = Open(path, MODE_OLDFILE)))) {
  86.                 ErrorHandleDos(PRGNAME, 1, 0L, path);
  87.                 my_exit(5);
  88.         }
  89.  
  90.         Seek(fp, 0L, OFFSET_END);
  91.         linecount = Seek(fp, 0L, OFFSET_CURRENT);
  92.  
  93.         while (TRUE) {
  94.                 Seek(fp, Random(linecount), OFFSET_BEGINNING);
  95.  
  96.                 while ((character = FGetC(fp)) != EOF) {
  97.                         if (character == '\n')
  98.                                 break;
  99.                         continue;
  100.                 }
  101.  
  102.                 if (!(FGetS(officialrule, fp)))
  103.                         continue;
  104.                 else {
  105.                         if (*officialrule == ' ' || *officialrule == '#')
  106.                                 continue;
  107.                         PrintFSimple("\n");
  108.                         print_rule(MARGIN, officialrule);
  109.                 }
  110.  
  111.                 next = 0;
  112.                 while (TRUE) {
  113.                         if (!(FGetS(officialrule, fp)))
  114.                                 my_exit(0);
  115.                         else {
  116.                                 if (*officialrule != ' ' || *officialrule == '\n')
  117.                                         my_exit(0);
  118.                                 print_rule(MARGIN, officialrule);
  119.                                 if (next++ >= 10)
  120.                                         my_exit(0);
  121.                         }
  122.                         continue;
  123.                 }
  124.         }
  125. }
  126.  
  127. /**************************************************************************
  128.  *                                                                        *
  129.  * Sektion: Unterprogramme                                                *
  130.  *                                                                        *
  131.  **************************************************************************/
  132.  
  133. void my_exit(int num)
  134. {
  135.         PrintFSimple("\n");
  136.         CloseLibrary((struct Library *) PToolBase); PToolBase = NULL;
  137.         exit(num);
  138. }
  139.  
  140. void print_rule(int number, char *rule)
  141. {
  142.         int offset = 0, i;
  143.         char fortune[BUFFERSIZE];
  144.  
  145.         *fortune = '\0';
  146.  
  147.         rule--;
  148.         while (*(++rule)) {
  149.                 i = strlen(fortune);
  150.                 *(fortune + i++) = *rule;
  151.                 *(fortune + i) = NULL;
  152.                 offset++;
  153.         }
  154.  
  155.         offset = ((number - offset) / 2);
  156.         for (; offset > 0; strins(fortune, " "), offset--) ;
  157.         PrintFSimple("%s", fortune);
  158. }
  159.  
  160. int FGetS(char *string, BPTR fp)
  161. {
  162.         int c;
  163.  
  164.         do {
  165.                 if ((c = FGetC(fp)) == EOF)
  166.                         return 0;
  167.                 *(string++) = c;
  168.         } while (c != '\n');
  169.  
  170.         *string = '\0';
  171.  
  172.         return 1;
  173. }
  174.  
  175.